import Vue from 'vue'
import Router from 'vue-router'
import iview from 'iview';
import store from '../store/';

import {Home,NotFound,Index,Login,Inspect,Peport,System,Jump} from "@/components/";


Vue.use(Router)


const indexRouter = [
  {
    path: '/',
    name: '首页',
    component: Index
  },{
    path:'/jump',
    name:'中转',
    component:Jump
  }
  ,{
    path: '/login',
    name: '登录',
    component: Login.Login,
  }, 
  {
    path: '/register',
    name: '注册',
    component: Login.Register,
  },
  {
    path:'/system',
    name:'系统管理',
    meta: { requiresAuth: true },
    component:System.System,
    children:[
      {
        path:'user',
        name:'用户管理',
        component:System.User
      },
      {
        path:'mechanism',
        name:'匹配税务编码',
        component:System.Mechanism
      },
      {
        path:'bookset',
        name:'设置默认商品类别',
        component:System.Bookset
      }
    ]
  },
  {
    path:'/peport',
    name:'资讯中心',
    meta: { requiresAuth: true },
    component:Peport.Peport,
    children:[
      {
        path:'statistics',
        name:'新闻列表',
        component:Peport.Statistics
      },
      {
        path:'newsadd',
        name:'新闻编辑',
        component:Peport.Newsadd
      },
      {
        path:'finance',
        name:'财务报表',
        component:Peport.Finance
      }
    ]
  },
  {
    path:'/inspect',
    name:'进项管理',
    meta: { requiresAuth: true },
    component:Inspect.Inspect,
    children:[
      {
        path:'auth',
        name:'发票认证、查验',
        component:Inspect.Auth
      },
      {
        path:'check',
        name:'发票查验',
        component:Inspect.Check
      },
      {
        path:'entry',
        name:'进项发票录入',
        component:Inspect.Entry
      },
      {
        path:'makeup',
        name:'进项发票使用明细',
        component:Inspect.Makeup
      }
    ]
  },
  {
    path:'/home',
    name:'销项管理',
    meta: { requiresAuth: true },
    component:Home.Home,
    children:[
      {
        path:'library',
        name:'电票开具',
        component:Home.Library
      },
      {
        path:'settlement',
        name:'普票开具',
        component:Home.Settlement
      },
      {
        path:'manual',
        name:'手动开票',
        component:Home.Manual
      },
      {
        path:'relation',
        name:'关联',
        component:Home.Relation
      },
      {
        path:'input',
        name:'发票录入',
        component:Home.Input
      },
      {
        path:'admin',
        name:'发票管理',
        component:Home.Admin
      },
      {
        path:'paper',
        name:'纸质发票物流跟踪',
        component:Home.Paper
      }
    ]
  }, {
    path: '/404',
    component: NotFound,
  }
];

const routes=indexRouter;

// 页面刷新时，重新赋值token
// if (window.localStorage.getItem('token')) {
//     store.commit(types.LOGIN, window.localStorage.getItem('token'))
// }

const router = new Router({
  //mode: 'history',
  routes
});

router.beforeEach((to, from, next) => {
    console.log(store.getters.getToken);
    window.scroll(0, 0);
    if (to.matched.some(record => record.meta.requiresAuth)) {
      if (!store.getters.getToken) {
          iview.Message.info('请登录！');
          return next({
              path: '/login',
              query: { redirect: to.fullPath }
          })
      }
    }
    iview.LoadingBar.start();
    next() // 确保一定要调用 next()
})
router.afterEach(transition => {

  iview.LoadingBar.finish();
});
export default router;
